#include <stdio.h>
#define size 3
#include <stdbool.h>
// 初始化
void Initboard(char (*board)[size])
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            board[i][j] = ' ';
        }
    }
}

// 打印期盼
void PrintBoard(char (*board)[size])
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            printf(" %c", board[i][j]);
            if (j < size - 1)
                printf("|");
        }
        printf("\n");
        if (i < size - 1)
            printf("--|--|--\n");
    }
}
// 下棋动作
bool Makemove(char (*board)[size], int row, int column, char player)
{ // 非法位置
    if (row < 0 || row > size - 1 || column > size - 1 || column < 0)
    {
        printf("非法的位置！请重新输入！\n");
        return false;
    }
    if (board[row][column] != ' ')
    {
        printf("这个位置已经有棋子了，请重新输入！");
        return false;
    }
    board[row][column] = player;
    return true;
}
// 判胜
bool Checkswin(char (*board)[size], char player)
{
    for (int i = 0; i < size; i++)
    {
        if (board[i][0] == player &&
            board[i][1] == player &&
            board[i][2] == player)
        {
            return true;
        }
    }
    for (int i = 0; i < size; i++)
    {
        if (board[0][i] == player &&
            board[1][i] == player &&
            board[2][i] == player)
        {
            return true;
        }
    }
    for (int i = 0; i < size; i++)
    {
        if (board[1][0] == player &&
            board[2][1] == player &&
            board[3][2] == player)
        {
            return true;
        }
    }
    for (int i = 0; i < size; i++)
    {
        if (board[0][0] == player &&
            board[1][1] == player &&
            board[2][2] == player)
        {
            return true;
        }
    }
    if (board[0][2] == player &&
        board[1][1] == player &&
        board[2][0] == player)
    {
        return true;
    }

    return false;
}
// 判平
bool CheckIsDraw(char (*board)[size])
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size; j++)
        {
            if (board[i][j] == ' ')
            {
                return false;
            }
        }
    }
    return true;
}

int main()
{
    char board[size][size];
    Initboard(board);
    char player = 'x';

    while (1)
    {
        PrintBoard(board);
        int row = 0, column = 0;
        printf("请|%c|下棋", player);
        // 阻塞形函数:让函数等待条件满足
        scanf("%d %d", &row, &column);
        if (Makemove(board, row, column, player) == false)
        {
            continue;
        }
        if (Checkswin(board, player) == true)
        {
            PrintBoard(board);
            printf("恭喜|%c|获胜", player);
            break;
        }
        if (CheckIsDraw(board) == true)
        {
            PrintBoard(board);
            printf("平局！\n");
            break;
        }
        if (player == 'x')
        {
            player = 'o';
        }
        else if (player == 'o')
        {
            player = 'x';
        }
    }

    return 0;
}